package com.jcfk.eam.controller.csm;

import cn.hutool.core.util.StrUtil;
import com.alibaba.excel.EasyExcel;
import com.jcfk.common.business.log.annotation.RequestLogHeader;
import com.jcfk.common.core.utils.ExcelUtils;
import com.jcfk.common.web.domain.*;
import com.jcfk.eam.domain.dto.csm.CsmProductRecordDTO;
import com.jcfk.eam.domain.excel.csm.CsmProductRecordExcel;
import com.jcfk.eam.domain.query.csm.CsmProductRecordQuery;
import com.jcfk.eam.domain.uploadExcel.csm.CsmProductRecordUploadExcel;
import com.jcfk.eam.service.csm.CsmProductRecordService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
 * 耗材生产记录
 *
 * @author yrl
 * @email
 * @date 2024-11-05
 */
@Api(tags = {"耗材生产记录管理"})
@RestController
@RequestMapping("csmproductrecord")
public class CsmProductRecordController {
    @Autowired
    private CsmProductRecordService csmProductRecordService;

    /**
     * 分页列表
     */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "ReqPageParam<CsmProductRecordQuery>", name = "requestParam", value = "", required = true)
    })
    @ApiOperation("分页查询耗材生产记录")
    @PostMapping("/queryByPage")
    public RetPage<CsmProductRecordDTO> queryByPage(@RequestBody ReqPageParam<CsmProductRecordQuery> requestParam) {
        //设置默认排序
        if(requestParam.getSort().getOrderItem()==null){
            ReqSortParam<CsmProductRecordQuery> sortParam=new ReqSortParam();
            sortParam.setColumn("lastUpdateTime");
            sortParam.setOrderBy("desc");
            requestParam.setSort(sortParam);
        }
        DataPage<CsmProductRecordDTO> page = new DataPage<CsmProductRecordDTO>((long) requestParam.getCurrent(),
                (long) requestParam.getSize(),
                requestParam.getSort().getOrderItem(),
            CsmProductRecordDTO.class);
        page = csmProductRecordService.queryByPage(page, requestParam.getData());

        return new RetPage<>(page);
    }


    /**
     *  耗材生产记录
     * 根据id 查询详情
     */
    @ApiOperation("查询耗材生产记录")
    @GetMapping("/get/{id}")
    public RetResult<CsmProductRecordDTO> get(@PathVariable("id") String id) {
            CsmProductRecordDTO item = csmProductRecordService.get(id);
        return RetResult.ok(item);
    }

    /**
     * 添加/更新管理耗材生产记录
     */
    @ApiOperation("保存耗材生产记录")
    @PostMapping("/save")
    @RequestLogHeader(modular = "耗材生产记录管理", businessType = "保存")
    public RetResult<?> save(@RequestBody @Valid CsmProductRecordDTO csmProductRecord) {
        // 数据校验
        RetResult ret = csmProductRecordService.isValid(csmProductRecord);
        if (!ret.getCode().equals(RetResult.SUCCESS)) {
            return ret;
        }

        if (StrUtil.isEmpty(csmProductRecord.getId())){
            // 新增
            csmProductRecordService.insert(csmProductRecord);
        } else{
            // 更新
            csmProductRecordService.update(csmProductRecord);
        }
        return RetResult.ok();

    }

    /**
     *  批量删除耗材生产记录
     */
    @ApiOperation("批量删除耗材生产记录")
    @PostMapping("/delete")
    @RequestLogHeader(modular = "耗材生产记录管理", businessType = "批量删除")
    public RetResult<?> delete(@RequestBody CsmProductRecordDTO csmProductRecord) {
        return RetResult.ok(csmProductRecordService.logicDeleteList(csmProductRecord));
    }


    @PostMapping("export")
    @ApiOperation("导出")
    public void export(@RequestBody CsmProductRecordQuery requestParam, HttpServletResponse response) throws Exception {
        List<CsmProductRecordDTO> list = csmProductRecordService.getList(requestParam);
        ExcelUtils.exportExcelToTarget(response, null, list, CsmProductRecordExcel.class);
    }

    /**
     * 导入耗材生产记录
     */
    @PostMapping("upload")
    public RetResult<?> upload(MultipartFile file) throws IOException {
        // 读取excel
        List<CsmProductRecordUploadExcel> items = EasyExcel.read(file.getInputStream(),CsmProductRecordUploadExcel.class,null).autoCloseStream(true).sheet(0).headRowNumber(1).autoTrim(true).doReadSync();
        csmProductRecordService.insertBatch(items);
        return RetResult.ok();
    }

    /**
     * 下载模板
     */
    @PostMapping("exportTemp")
    @ApiOperation("下载模板")
    public void exportTemp(HttpServletResponse response) throws Exception {
        List<CsmProductRecordUploadExcel> list = new ArrayList<>();
        list.add(new CsmProductRecordUploadExcel());
        ExcelUtils.exportExcelToTarget(response, null, list, CsmProductRecordUploadExcel.class);
    }


    /**
     *  删除耗材生产记录
     */
    @ApiOperation("删除耗材生产记录")
    @PostMapping("/deleteItem/{id}")
    @RequestLogHeader(modular = "耗材生产记录管理", businessType = "删除")
    public RetResult<?> deleteItem(@PathVariable("id") String id) {
        csmProductRecordService.deleteItem(id);
        return RetResult.ok();
    }
}
